* This is the do file to create "Table 1. Summary Statistics"
set seed 123

use "$path_data/temp/followup_student_parents_matched", clear

corr rosen_pre_std cpcs_pre_std
corr RSES_std CPCS_std


/// Varable Selection
/// Baseline
foreach j in n mean sd {
tabstat DT_score_pre_std rosen_pre_std cpcs_pre_std if treatment == 1, stat(`j') save
matrix list r(StatTotal)
matrix `j'_tr_bl = r(StatTotal)

tabstat DT_score_pre_std rosen_pre_std cpcs_pre_std if treatment == 0, stat(`j') save
matrix list r(StatTotal)
matrix `j'_ct_bl = r(StatTotal)
}

matrix n_bl = J(1,3,.)
forvalues i = 1/3 {
	matrix n_bl[1,`i'] = n_tr_bl[1,`i'] + n_ct_bl[1,`i']
}

foreach dep in DT_score_pre_std rosen_pre_std cpcs_pre_std{
    
    wildbootstrap reg `dep' treatment, cluster(school_no) reps(1000)
	
	matrix r2_`dep'_temp = r(table)


    foreach s in mean se pv obs {
		matrix r2_`dep'_`s' = J(1,2,.)
	}

	foreach j in 1 2 {
	* beta
	matrix r2_`dep'_mean[1,`j'] = r2_`dep'_temp[1,`j']
	* standard error
	* matrix r2_`dep'_se[1,`j'] = r2_`dep'_temp[2,`j']
	* p value
	matrix r2_`dep'_pv[1,`j'] = r2_`dep'_temp[3,`j']
	}

}

/// Family
foreach j in n mean sd {
tabstat hhmember hhheadage hhheadeduyear if treatment == 1, stat(`j') save
matrix list r(StatTotal)
matrix `j'_tr_parent = r(StatTotal)

tabstat hhmember hhheadage hhheadeduyear if treatment == 0, stat(`j') save
matrix list r(StatTotal)
matrix `j'_ct_parent = r(StatTotal)
}

matrix n_parent = J(1,3,.)
forvalues i = 1/3 {
	matrix n_parent[1,`i'] = n_tr_parent[1,`i'] + n_ct_parent[1,`i']
}

foreach dep in hhmember hhheadage hhheadeduyear{
    
    wildbootstrap reg `dep' treatment, cluster(school_no) reps(1000)
	
	matrix r2_`dep'_temp = r(table)


    foreach s in mean se pv obs {
		matrix r2_`dep'_`s' = J(1,2,.)
	}

	foreach j in 1 2 {
	* beta
	matrix r2_`dep'_mean[1,`j'] = r2_`dep'_temp[1,`j']
	* standard error
	* matrix r2_`dep'_se[1,`j'] = r2_`dep'_temp[2,`j']
	* p value
	matrix r2_`dep'_pv[1,`j'] = r2_`dep'_temp[3,`j']
	}

}



/// School　attendance
foreach j in n mean sd {
tabstat q2a q2b q2c q2h if treatment == 1, stat(`j') save
matrix list r(StatTotal)
matrix `j'_tr_school = r(StatTotal)

tabstat q2a q2b q2c q2h if treatment == 0, stat(`j') save
matrix list r(StatTotal)
matrix `j'_ct_school = r(StatTotal)
}

matrix n_school = J(1,4,.)
forvalues i = 1/4 {
	matrix n_school[1,`i'] = n_tr_school[1,`i'] + n_ct_school[1,`i']
}

foreach dep in q2a q2b q2c q2h{
    
    wildbootstrap reg `dep' treatment, cluster(school_no) reps(1000)
	
	matrix r2_`dep'_temp = r(table)


    foreach s in mean se pv obs {
		matrix r2_`dep'_`s' = J(1,2,.)
	}

	foreach j in 1 2 {
	* beta
	matrix r2_`dep'_mean[1,`j'] = r2_`dep'_temp[1,`j']
	* standard error
	* matrix r2_`dep'_se[1,`j'] = r2_`dep'_temp[2,`j']
	* p value
	matrix r2_`dep'_pv[1,`j'] = r2_`dep'_temp[3,`j']
	}

}

/// Other study variable
foreach j in n mean sd {
tabstat tutor study_other study_affect_covid hometutoring onlineclass studymyself parentsteach if treatment == 1, stat(`j') save
matrix list r(StatTotal)
matrix `j'_tr_study = r(StatTotal)

tabstat tutor study_other study_affect_covid hometutoring onlineclass studymyself parentsteach if treatment == 0, stat(`j') save
matrix list r(StatTotal)
matrix `j'_ct_study = r(StatTotal)
}

matrix n_study = J(1,8,.)
forvalues i = 1/8 {
	matrix n_study[1,`i'] = n_tr_study[1,`i'] + n_ct_study[1,`i']
}

foreach dep in tutor study_other study_affect_covid hometutoring onlineclass studymyself parentsteach{
    
    wildbootstrap reg `dep' treatment, cluster(school_no) reps(1000)
	
	matrix r2_`dep'_temp = r(table)


    foreach s in mean se pv obs {
		matrix r2_`dep'_`s' = J(1,2,.)
	}

	foreach j in 1 2 {
	* beta
	matrix r2_`dep'_mean[1,`j'] = r2_`dep'_temp[1,`j']
	* standard error
	* matrix r2_`dep'_se[1,`j'] = r2_`dep'_temp[2,`j']
	* p value
	matrix r2_`dep'_pv[1,`j'] = r2_`dep'_temp[3,`j']
	}

}

/// Cognitive
foreach j in n mean sd {
tabstat followup_cog_std if treatment == 1, stat(`j') save
matrix list r(StatTotal)
matrix `j'_tr_cog = r(StatTotal)

tabstat followup_cog_std if treatment == 0, stat(`j') save
matrix list r(StatTotal)
matrix `j'_ct_cog = r(StatTotal)
}

matrix n_cog = J(1,1,.)
forvalues i = 1/1 {
	matrix n_cog[1,`i'] = n_tr_cog[1,`i'] + n_ct_cog[1,`i']
}

wildbootstrap reg followup_cog_std treatment, cluster(school_no) reps(1000)
	
matrix r2_followup_cog_std_temp = r(table)


foreach s in mean se pv obs {
		matrix r2_followup_cog_std_`s' = J(1,2,.)
}

foreach j in 1 2 {
* beta
matrix r2_followup_cog_std_mean[1,`j'] = r2_followup_cog_std_temp[1,`j']
* standard error
* matrix r2_followup_cog_std_se[1,`j'] = r2_followup_cog_std_temp[2,`j']
* p value
matrix r2_followup_cog_std_pv[1,`j'] = r2_followup_cog_std_temp[3,`j']
}
    
/// Non cognitive
foreach j in n mean sd {
tabstat followup_noncog_std RSES_std CPCS_std if treatment == 1, stat(`j') save
matrix list r(StatTotal)
matrix `j'_tr_noncog = r(StatTotal)

tabstat followup_noncog_std RSES_std CPCS_std if treatment == 0, stat(`j') save
matrix list r(StatTotal)
matrix `j'_ct_noncog = r(StatTotal)
}

matrix n_noncog = J(1,3,.)
forvalues i = 1/3 {
	matrix n_noncog[1,`i'] = n_tr_noncog[1,`i'] + n_ct_noncog[1,`i']
}

foreach dep in followup_noncog_std RSES_std CPCS_std{
    
    wildbootstrap reg `dep' treatment, cluster(school_no) reps(1000)
	
	matrix r2_`dep'_temp = r(table)


    foreach s in mean se pv obs {
		matrix r2_`dep'_`s' = J(1,2,.)
	}

	foreach j in 1 2 {
	* beta
	matrix r2_`dep'_mean[1,`j'] = r2_`dep'_temp[1,`j']
	* standard error
	* matrix r2_`dep'_se[1,`j'] = r2_`dep'_temp[2,`j']
	* p value
	matrix r2_`dep'_pv[1,`j'] = r2_`dep'_temp[3,`j']
	}

}


/// Behavioral
foreach j in n mean sd {
tabstat hyper if treatment == 1, stat(`j') save
matrix list r(StatTotal)
matrix `j'_tr_hyper = r(StatTotal)

tabstat hyper if treatment == 0, stat(`j') save
matrix list r(StatTotal)
matrix `j'_ct_hyper = r(StatTotal)
}

matrix n_hyper = J(1,1,.)
forvalues i = 1/1 {
	matrix n_hyper[1,`i'] = n_tr_hyper[1,`i'] + n_ct_hyper[1,`i']
}

foreach dep in hyper{
    
    wildbootstrap reg `dep' treatment if hypernoinfo == 0, cluster(school_no) reps(1000)
	
	matrix r2_`dep'_temp = r(table)


    foreach s in mean se pv obs {
		matrix r2_`dep'_`s' = J(1,2,.)
	}

	foreach j in 1 2 {
	* beta
	matrix r2_`dep'_mean[1,`j'] = r2_`dep'_temp[1,`j']
	* standard error
	* matrix r2_`dep'_se[1,`j'] = r2_`dep'_temp[2,`j']
	* p value
	matrix r2_`dep'_pv[1,`j'] = r2_`dep'_temp[3,`j']
	}

}


// significant level

local outcome DT_score_pre_std rosen_pre_std cpcs_pre_std hhmember hhheadage hhheadeduyear q2a q2c q2h tutor study_other followup_cog_std RSES_std CPCS_std

foreach dep in `outcome'{
		if r2_`dep'_pv[1,1]<=0.01 {
			local star_`dep' %3s "***"
		}
		else if (r2_`dep'_pv[1,1]>0.01) & (r2_`dep'_pv[1,1]<=0.05) {
			local star_`dep' %2s "**"
		}
		else if (r2_`dep'_pv[1,1]>0.05) & (r2_`dep'_pv[1,1]<=0.10) {
			local star_`dep' %1s "*"
		}
		else {
			local star_`dep'  ""
		}
} 

rwolf DT_score_pre_std rosen_pre_std cpcs_pre_std hhmember hhheadage hhheadeduyear, indepvar(treatment) reps(1000)
rwolf q2a q2c q2h tutor study_other followup_cog_std RSES_std CPCS_std, indepvar(treatment) reps(1000)


/// Table
tempname hh2
file open `hh2' using "$path_output/summary_stat.tex", write replace
file write `hh2' "" _newline
file write `hh2' "% Author: Kazuma Takakura" _newline
file write `hh2' "% Date: `c(current_date)'" _newline
file write `hh2' "% Time: `c(current_time)'" _newline
file write `hh2' "" _newline


file write `hh2' "\begin{table}[h!]\footnotesize" _newline
file write `hh2' "  \centering" _newline
file write `hh2' "  \caption{Summary Statistics}" _newline
file write `hh2' "\label{tab:sumstat}" _newline
file write `hh2' "\scalebox{1}{" _newline
file write `hh2' "\begin{threeparttable}" _newline

file write `hh2' "\begin{tabular}{lccccc}\toprule" _newline

  
file write `hh2' " Dependent Variable & Treatment &  Control  & Difference & N   \\\midrule\midrule" _newline
file write `hh2' " Panel A: Baseline & & & &   \\ " _newline
file write `hh2' " DT score^{a} & " %04.3f (mean_tr_bl[1,1]) " & " %04.3f (mean_ct_bl[1,1]) " & " %04.3f (r2_DT_score_pre_std_mean[1,1]) `star_DT_score_pre_std' " & " (n_bl[1,1]) "  \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_bl[1,1]) " ] & [ " %04.3f (sd_ct_bl[1,1]) " ] & ( " %04.3f (r2_DT_score_pre_std_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.831) " \} &   \\ " _newline


file write `hh2' " RSES score^{a} & " %04.3f (mean_tr_bl[1,2]) " & " %04.3f (mean_ct_bl[1,2]) " & " %04.3f (r2_rosen_pre_std_mean[1,1]) `star_rosen_pre_std' " & "  (n_bl[1,2]) " \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_bl[1,2]) " ] & [ " %04.3f (sd_ct_bl[1,2]) " ] & ( " %04.3f (r2_rosen_pre_std_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.831) " \} &   \\ " _newline

file write `hh2' " CPCS score^{a} & " %04.3f (mean_tr_bl[1,3]) " & " %04.3f (mean_ct_bl[1,3]) " & " %04.3f (r2_cpcs_pre_std_mean[1,1]) `star_cpcs_pre_std' " & "  (n_bl[1,3]) " \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_bl[1,3]) " ] & [ " %04.3f (sd_ct_bl[1,3]) " ] & ( " %04.3f (r2_cpcs_pre_std_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.059) " \} &   \\ " _newline

file write `hh2' " Household size & " %04.3f (mean_tr_parent[1,1]) " & " %04.3f (mean_ct_parent[1,1]) " & " %04.3f (r2_hhmember_mean[1,1]) `star_hhmember'  " & " (n_parent[1,1]) "  \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_parent[1,1]) " ] & [ " %04.3f (sd_ct_parent[1,1]) " ] & ( " %04.3f (r2_hhmember_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.464) " \} &   \\ " _newline

file write `hh2' " Household head age & " %04.3f (mean_tr_parent[1,2]) " & " %04.3f (mean_ct_parent[1,2]) " & " %04.3f (r2_hhheadage_mean[1,1]) `star_hhheadage' " & "  (n_parent[1,2]) " \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_parent[1,2]) " ] & [ " %04.3f (sd_ct_parent[1,2]) " ] & ( " %04.3f (r2_hhheadage_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.920) " \} &   \\ " _newline

file write `hh2' " Household head education & " %04.3f (mean_tr_parent[1,3]) " & " %04.3f (mean_ct_parent[1,3]) " & " %04.3f (r2_hhheadeduyear_mean[1,1]) `star_hhheadeduyear' " & "  (n_parent[1,3]) "  \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_parent[1,3]) " ] & [ " %04.3f (sd_ct_parent[1,3]) " ] & ( " %04.3f (r2_hhheadeduyear_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.248) " \} &   \\ " _newline
file write `hh2' " \\ "_newline

file write `hh2' " Panel B: Follow-up & & & &   \\ " _newline

file write `hh2' " School attendance & " %04.3f (mean_tr_school[1,1]) " & " %04.3f (mean_ct_school[1,1]) " & " %04.3f (r2_q2a_mean[1,1]) `star_q2a' " & " (n_school[1,1]) "  \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_school[1,1]) " ] & [ " %04.3f (sd_ct_school[1,1]) " ] & ( " %04.3f (r2_q2a_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.787) " \} &   \\ " _newline

file write `hh2' " Grade repeat & " %04.3f (mean_tr_school[1,3]) " & " %04.3f (mean_ct_school[1,3]) " & " %04.3f (r2_q2c_mean[1,1]) `star_q2c' " & "  (n_school[1,3]) " \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_school[1,3]) " ] & [ " %04.3f (sd_ct_school[1,3]) " ] & ( " %04.3f (r2_q2c_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.787) " \} &   \\ " _newline

file write `hh2' " Drop out & " %04.3f (mean_tr_school[1,4]) " & " %04.3f (mean_ct_school[1,4]) " & " %04.3f (r2_q2h_mean[1,1]) `star_q2h'  " & "  (n_school[1,4]) "  \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_school[1,4]) " ] & [ " %04.3f (sd_ct_school[1,4]) " ] & ( " %04.3f (r2_q2h_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.576) " \} &   \\ " _newline

file write `hh2' " Tutoring & " %04.3f (mean_tr_study[1,1]) " & " %04.3f (mean_ct_study[1,1]) " & " %04.3f (r2_tutor_mean[1,1]) `star_tutor'  " & " (n_study[1,1]) "  \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_study[1,1]) " ] & [ " %04.3f (sd_ct_study[1,1]) " ] & ( " %04.3f (r2_tutor_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.230) " \} &   \\ " _newline

file write `hh2' " Self-study & " %04.3f (mean_tr_study[1,2]) " & " %04.3f (mean_ct_study[1,2]) " & " %04.3f (r2_study_other_mean[1,1]) `star_study_other' " & "  (n_study[1,2]) " \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_study[1,2]) " ] & [ " %04.3f (sd_ct_study[1,2]) " ] & ( " %04.3f (r2_study_other_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.787) " \} &   \\ " _newline

file write `hh2' " Rapid math test score^{a} & " %04.3f (mean_tr_cog[1,1]) " & " %04.3f (mean_ct_cog[1,1]) " & " %04.3f (r2_followup_cog_std_mean[1,1]) `star_followup_cog_std'  "  & "  (n_cog[1,1]) "  \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_cog[1,1]) " ] & [ " %04.3f (sd_ct_cog[1,1]) " ] & ( " %04.3f (r2_followup_cog_std_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.270) " \} &   \\ " _newline

file write `hh2' " RSES score^{a} & " %04.3f (mean_tr_noncog[1,2]) " & " %04.3f (mean_ct_noncog[1,2]) " & " %04.3f (r2_RSES_std_mean[1,1])   `star_RSES_std' " & " (n_noncog[1,2]) "  \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_noncog[1,2]) " ] & [ " %04.3f (sd_ct_noncog[1,2]) " ] & ( " %04.3f (r2_RSES_std_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.011) " \} &   \\ " _newline

file write `hh2' " CPCS score^{a} & " %04.3f (mean_tr_noncog[1,3]) " & " %04.3f (mean_ct_noncog[1,3]) " & " %04.3f (r2_CPCS_std_mean[1,1])   `star_CPCS_std' "&  " (n_noncog[1,3]) " \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_tr_noncog[1,3]) " ] & [ " %04.3f (sd_ct_noncog[1,3]) " ] & ( " %04.3f (r2_CPCS_std_pv[1,1]) " ) &   \\ " _newline
file write `hh2' "    				 &   &   & \{ " %04.3f (0.006) " \} &   \\ " _newline


file write `hh2' "\midrule" _newline
file write `hh2' "\end{tabular}" _newline
file write `hh2' "\begin{tablenotes}" _newline
file write `hh2' "\item (a) Variables are standardized using the average and variance of the whole follow-up sample in the February 2022 survey. " _newline
file write `hh2' "\item (b) Standard deviations are reported in square brackets.  " _newline
file write `hh2' "\item (c) Wild clustered bootstrap p-values are reported within parentheses. Clusters are schools at the baseline. There are 34 clusters. " _newline
file write `hh2' "\item (d) Romano-Wolf multiple hypothesis testing p-values are reported in curly brackets. This test is conducted separately for the baseline variables and the follow-up variables." _newline
file write `hh2' "\item (e) Statistical significance is indicated by stars based on the wild clustered bootstrap p-values reported in parentheses: $*$ denotes significance at the 10\% level, $∗∗$ at the 5\% level, and $∗∗∗$ at the 1\% level.  " _newline
file write `hh2' "\end{tablenotes}" _newline
file write `hh2' "\end{threeparttable}" _newline
file write `hh2' "}" _newline
file write `hh2' "\end{table}" _newline

file write `hh2' "" _newline
file write `hh2' "" _newline
file write `hh2' "" _newline
file write `hh2' "" _newline

file close `hh2'
